home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsimpath.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.0 KB  |  187 lines

  1. /* Copyright (C) 1989, 1992, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsimpath.c,v 1.2 2000/09/19 19:00:29 lpd Exp $ */
  20. /* Image to outline conversion for Ghostscript library */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gsmatrix.h"
  24. #include "gspaint.h"        /* for gs_imagepath prototype */
  25. #include "gsstate.h"
  26. #include "gspath.h"
  27.  
  28. /* Define the state of the conversion process. */
  29. typedef struct {
  30.     /* The following are set at the beginning of the conversion. */
  31.     gs_state *pgs;
  32.     const byte *data;        /* image data */
  33.     int width, height, raster;
  34.     /* The following are updated dynamically. */
  35.     int dx, dy;            /* X/Y increment of current run */
  36.     int count;            /* # of steps in current run */
  37. } status;
  38.  
  39. /* Define the scaling for the path tracer. */
  40. /* It must be even. */
  41. #define outline_scale 4
  42. /* Define the length of the short strokes for turning corners. */
  43. #define step 1
  44.  
  45. /* Forward declarations */
  46. private int get_pixel(P3(const status *, int, int));
  47. private int trace_from(P4(status *, int, int, int));
  48. private int add_dxdy(P4(status *, int, int, int));
  49.  
  50. #define add_deltas(s, dx, dy, n)\
  51.   if ( (code = add_dxdy(s, dx, dy, n)) < 0 ) return code
  52. /* Append an outline derived from an image to the current path. */
  53. int
  54. gs_imagepath(gs_state * pgs, int width, int height, const byte * data)
  55. {
  56.     status stat;
  57.     status *out = &stat;
  58.     int code, x, y;
  59.  
  60.     /* Initialize the state. */
  61.     stat.pgs = pgs;
  62.     stat.data = data;
  63.     stat.width = width;
  64.     stat.height = height;
  65.     stat.raster = (width + 7) / 8;
  66.     /* Trace the cells to form an outline.  The trace goes in clockwise */
  67.     /* order, always starting by going west along a bottom edge. */
  68.     for (y = height - 1; y >= 0; y--)
  69.     for (x = width - 1; x >= 0; x--) {
  70.         if (get_pixel(out, x, y) && !get_pixel(out, x, y - 1) &&
  71.           (!get_pixel(out, x + 1, y) || get_pixel(out, x + 1, y - 1)) &&
  72.         !trace_from(out, x, y, 1)
  73.         ) {        /* Found a starting point */
  74.         stat.count = 0;
  75.         stat.dx = stat.dy = 0;
  76.         if ((code = trace_from(out, x, y, 0)) < 0)
  77.             return code;
  78.         add_deltas(out, 0, 0, 1);    /* force out last segment */
  79.         if ((code = gs_closepath(pgs)) < 0)
  80.             return code;
  81.         }
  82.     }
  83.     return 0;
  84. }
  85.  
  86. /* Get a pixel from the data.  Return 0 if outside the image. */
  87. private int
  88. get_pixel(register const status * out, int x, int y)
  89. {
  90.     if (x < 0 || x >= out->width || y < 0 || y >= out->height)
  91.     return 0;
  92.     return (out->data[y * out->raster + (x >> 3)] >> (~x & 7)) & 1;
  93. }
  94.  
  95. /* Trace a path.  If detect is true, don't draw, just return 1 if we ever */
  96. /* encounter a starting point whose x,y follows that of the initial point */
  97. /* in x-then-y scan order; if detect is false, actually draw the outline. */
  98. private int
  99. trace_from(register status * out, int x0, int y0, int detect)
  100. {
  101.     int x = x0, y = y0;
  102.     int dx = -1, dy = 0;    /* initially going west */
  103.     int part = 0;        /* how far along edge we are; */
  104.  
  105.     /* initialized only to pacify gcc */
  106.     int code;
  107.  
  108.     if (!detect) {
  109.     part = (get_pixel(out, x + 1, y - 1) ?
  110.         outline_scale - step : step);
  111.     code = gs_moveto(out->pgs,
  112.              x + 1 - part / (float)outline_scale,
  113.              (float)y);
  114.     if (code < 0)
  115.         return code;
  116.     }
  117.     while (1) {            /* Relative to the current direction, */
  118.     /* -dy,dx is at +90 degrees (counter-clockwise); */
  119.     /* tx,ty is at +45 degrees; */
  120.     /* ty,-tx is at -45 degrees (clockwise); */
  121.     /* dy,-dx is at -90 degrees. */
  122.     int tx = dx - dy, ty = dy + dx;
  123.  
  124.     if (get_pixel(out, x + tx, y + ty)) {    /* Cell at 45 degrees is full, */
  125.         /* go counter-clockwise. */
  126.         if (!detect) {    /* If this is a 90 degree corner set at a */
  127.         /* 45 degree angle, avoid backtracking. */
  128.         if (out->dx == ty && out->dy == -tx) {
  129. #define half_scale (outline_scale / 2 - step)
  130.             out->count -= half_scale;
  131.             add_deltas(out, tx, ty, outline_scale / 2);
  132. #undef half_scale
  133.         } else {
  134.             add_deltas(out, dx, dy, step - part);
  135.             add_deltas(out, tx, ty, outline_scale - step);
  136.         }
  137.         part = outline_scale - step;
  138.         }
  139.         x += tx, y += ty;
  140.         dx = -dy, dy += tx;
  141.     } else if (!get_pixel(out, x + dx, y + dy)) {    /* Cell straight ahead is empty, go clockwise. */
  142.         if (!detect) {
  143.         add_deltas(out, dx, dy, outline_scale - step - part);
  144.         add_deltas(out, ty, -tx, step);
  145.         part = step;
  146.         }
  147.         dx = dy, dy -= ty;
  148.     } else {        /* Neither of the above, go in same direction. */
  149.         if (!detect) {
  150.         add_deltas(out, dx, dy, outline_scale);
  151.         }
  152.         x += dx, y += dy;
  153.     }
  154.     if (dx == -step && dy == 0 && !(tx == -step && ty == -step)) {    /* We just turned a corner and are going west, */
  155.         /* so the previous pixel is a starting point pixel. */
  156.         if (x == x0 && y == y0)
  157.         return 0;
  158.         if (detect && (y > y0 || (y == y0 && x > x0)))
  159.         return 1;
  160.     }
  161.     }
  162. }
  163.  
  164. /* Add a (dx, dy) pair to the path being formed. */
  165. /* Accumulate successive segments in the same direction. */
  166. private int
  167. add_dxdy(register status * out, int dx, int dy, int count)
  168. {
  169.     if (count != 0) {
  170.     if (dx == out->dx && dy == out->dy)
  171.         out->count += count;
  172.     else {
  173.         if (out->count != 0) {
  174.         int code = gs_rlineto(out->pgs,
  175.                 out->dx * out->count / (float)outline_scale,
  176.                    out->dy * out->count / (float)outline_scale);
  177.  
  178.         if (code < 0)
  179.             return code;
  180.         }
  181.         out->dx = dx, out->dy = dy;
  182.         out->count = count;
  183.     }
  184.     }
  185.     return 0;
  186. }
  187.